package com.x.client.activity

import android.support.v4.content.ContextCompat
import android.support.v7.widget.DividerItemDecoration
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.gg.baselibrary.BaseActivity
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.gg.utils.StatusUtils
import com.gg.widget.LoadUtils
import com.gg.widget.navigationbar.DefaultNavigationBar
import com.lsxiao.apollo.core.Apollo
import com.x.client.R
import com.x.client.bean.Province
import com.x.client.net.ApiFactory
import com.x.client.net.Composers
import com.x.client.utils.BusTag
import com.x.client.utils.RouterClass
import com.x.client.utils.RouterField
import kotlinx.android.synthetic.main.activity_recyclerview.*

/**
 *  Creator : GG
 *  Time    : 2017/12/1
 *  Mail    : gg.jin.yu@gmail.com
 *  Explain : 省份选择页面
 */
@Route(path = RouterClass.province)
class ProvinceActivity : BaseActivity() {

    @Autowired(name = RouterField.provinceId)
    @JvmField
    var mProvinceId: String = ""

    @Autowired(name = RouterField.cityId)
    @JvmField
    var mCityId: String = ""

    private var adapter: ProvinceAdapter = ProvinceAdapter()

    override fun getLayoutRes(): Int = R.layout.activity_recyclerview

    override fun initArguments() {
    }

    override fun initTitle() {
        StatusUtils.setStatusTextColor(true, this)
        StatusUtils.setStatusBar(this, false, false)
        DefaultNavigationBar.Builder(this)
                .setLeftIcon(R.drawable.ic_back_black)
                .setLeftClickListener (View.OnClickListener{ onBackPressed() })
                .setMiddleText("省份列表")
                .create()
    }

    override fun initView() {

    }

    override fun initData() {

        recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL).apply {
            setDrawable(ContextCompat.getDrawable(this@ProvinceActivity, R.drawable.divider_ll_grey_h)!!)
        })

        recyclerView.layoutManager = LinearLayoutManager(this)


        recyclerView.adapter = adapter

        adapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
            Apollo.emit(BusTag.province, adapter.data[position])
            ARouter.getInstance().build(RouterClass.city).withString(RouterField.provinceId,adapter.data[position].provinceId).withString(RouterField.cityId,mCityId).navigation()
//            startActivity(Intent(this@ProvinceActivity, CityListActivity::class.java).putExtra(CityListActivity.KEY_PROVINCE_ID, adapter.data[position].provinceId).putExtra(CityListActivity.KEY_CITY_ID, mCityId))
        }

       LoadUtils.show(this)
        ApiFactory.getErpApi()
                .provinceList()
                .compose(Composers.handleError())
                .compose(this.bindToLifecycle())
                .subscribe({
                    it.forEach {
                        if (it.provinceId == mProvinceId)
                            it.status = true
                    }
                    adapter.setNewData(it)
                    LoadUtils.hidden()
                }, { LoadUtils.hidden()})

    }

    private class ProvinceAdapter(data: List<Province> = listOf()) : BaseQuickAdapter<Province, BaseViewHolder>(R.layout.item_brand, data) {
        override fun convert(helper: BaseViewHolder, item: Province) {
            helper.setGone(R.id.logo, false)
            helper.setText(R.id.name, item.provinceName)
            if (item.status) {
                helper.setTextColor(R.id.name, ContextCompat.getColor(mContext, R.color.text_orange))
                helper.setVisible(R.id.check_mark, true)
            } else {
                helper.setTextColor(R.id.name, ContextCompat.getColor(mContext, R.color.text_black))
                helper.setVisible(R.id.check_mark, false)
            }
        }

    }
}